package cn.myfirefly.services.persondir;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import org.jasig.services.persondir.IPersonAttributeDao;
import org.jasig.services.persondir.IPersonAttributes;
import org.springframework.jdbc.core.JdbcTemplate;

public class CompanyPersonAttributeDao implements IPersonAttributeDao {

    private JdbcTemplate jdbcTemplate;
	
	private DataSource dataSource;
	
	private Set<String> possibleUserAttributeNames;
	
	public IPersonAttributes getPerson(String uid) {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<IPersonAttributes> getPeople(Map<String, Object> query) {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<IPersonAttributes> getPeopleWithMultivaluedAttributes(
			Map<String, List<Object>> query) {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<String> getPossibleUserAttributeNames() {
		return possibleUserAttributeNames;
	}

	public Set<String> getAvailableQueryAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, List<Object>> getMultivaluedUserAttributes(
			Map<String, List<Object>> seed) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, List<Object>> getMultivaluedUserAttributes(String uid) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, Object> getUserAttributes(Map<String, Object> seed) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, Object> getUserAttributes(String uid) {
		
//		String sql = "SELECT u.login_name, c.*,de.department_sn,d.department_name FROM xh_operator u, xh_employee c LEFT JOIN xh_depart_employee de  ON c.employee_sn = de.employee_sn LEFT JOIN xh_department d ON d.department_sn = de.department_sn WHERE u.status='1' AND u.employee_no=c.employee_no AND u.login_name= ? ";
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("SELECT ");
		sqlBuffer.append("  u.login_name, ");
		sqlBuffer.append("  u.user_name, ");
		sqlBuffer.append("  u.user_sn, ");
		sqlBuffer.append("  s.staff_sn, ");
		sqlBuffer.append("  s.staff_no, ");
		sqlBuffer.append("  s.name, ");
		sqlBuffer.append("  s.nick_name, ");
		sqlBuffer.append("  s.sex, ");
		sqlBuffer.append("  s.email, ");
		sqlBuffer.append("  s.address, ");
		sqlBuffer.append("  s.phone_no, ");
		sqlBuffer.append("  s.highest_education, ");
		sqlBuffer.append("  s.graduated_from, ");
		sqlBuffer.append("  s.profession, ");
		sqlBuffer.append("  s.credential_type, ");
		sqlBuffer.append("  s.credential_no, ");
		sqlBuffer.append("  s.position, ");
		sqlBuffer.append("  s.user_status, ");
		sqlBuffer.append("  s.join_date, ");
		sqlBuffer.append("  de.department_sn, ");
		sqlBuffer.append("  d.department_name ");
		sqlBuffer.append("FROM mm_user u ");
		sqlBuffer.append("  LEFT JOIN mm_staff s ");
		sqlBuffer.append("    ON u.user_sn=s.user_sn ");
		sqlBuffer.append("  LEFT JOIN mm_department_staff de ");
		sqlBuffer.append("    ON s.staff_sn = de.staff_sn ");
		sqlBuffer.append("  LEFT JOIN mm_department d ");
		sqlBuffer.append("    ON d.department_sn = de.department_sn ");
		sqlBuffer.append("WHERE u.status = '1' ");
		sqlBuffer.append("    AND u.login_name = ? ");

        final Map<String, Object> personMap = jdbcTemplate.queryForMap(sqlBuffer.toString(), uid);
		if (personMap != null && possibleUserAttributeNames != null) {
			possibleUserAttributeNames = personMap.keySet();
		}
        return personMap;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
		this.dataSource = dataSource;
	}

}
